<?php
class Access
{
    private $_controllerName;
    private $_methodName;
    private $_requestUrl;
    private $_acl;
    private $_protectedUrlList = array();
    public static $_ANONYMOUS = "anonymous";
    private $_canAccess = false;

    function __construct($controllerName, $methodName)
    {
        $this->_controllerName = $controllerName;
        $this->_methodName = $methodName;
        $this->_acl = $GLOBALS['config']['user_url_acl'];
        $this->_requestUrl = $GLOBALS['_SERVER']['REQUEST_URI'];
        $this->initProtectedUrls();
    }

    public function route()
    {
        if (file_exists(ROOT . "/controller/" . $this->_controllerName . ".Controller.php")) {
            if (in_array("/" . $this->_controllerName . "/**", $this->_protectedUrlList)) {

                if (in_array($this->_requestUrl, $GLOBALS['config']['user_url_acl_exclude'])) {
                    $this->_canAccess = true;
                } else {
                    if ($this->getCurrentUserRole() == Access::$_ANONYMOUS) {
                        Boot::redirect("/");
                    } else {
                        $protectedUrl = $GLOBALS['config']['user_url_acl'][$this->getCurrentUserRole()];
                        foreach ($protectedUrl as $url) {
                            $url = str_replace("/**", "", $url);
                            if (strpos($this->_requestUrl, $url) > -1) {
                                $this->_canAccess = true;
                                break;
                            }
                        }
                    }
                }


                if (!$this->_canAccess) {
                    Boot::redirect("/");
                }
            }
            include_once(ROOT . "/controller/Controller.php");
            include_once(ROOT . "/controller/" . $this->_controllerName . ".controller.php");
            $controllerName = $this->_controllerName . "Controller";
            call_user_func(array(new $controllerName, $this->_methodName));
        } else {
            die("404");
        }
    }

    private function initProtectedUrls()
    {
        foreach ($this->_acl as $role => $acl) {
            foreach ($acl as $acl) {
                array_push($this->_protectedUrlList, $acl);
                continue;
            }
            array_push($this->_protectedUrlList, $acl);
        }

    }

    //取得当前角色，游客为anonymous
    private function getCurrentUserRole()
    {

        if (!array_key_exists("credentials", $_SESSION) || $_SESSION['credentials']['userId'] == 0) return "anonymous";
        $credentials = $_SESSION['credentials'];
        return $credentials['type'];
    }

    public function setControllerName($controllerName)
    {
        $this->_controllerName = $controllerName;
    }

    public function getControllerName()
    {
        return $this->_controllerName;
    }

    public function setMethodName($methodName)
    {
        $this->_methodName = $methodName;
    }

    public function getMethodName()
    {
        return $this->_methodName;
    }

}

?>